package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppCityService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppCityCreateValidate;
import com.mdd.admin.validate.yylq.TAppCitySearchValidate;
import com.mdd.admin.validate.yylq.TAppCityUpdateValidate;
import com.mdd.admin.vo.yylq.TAppCityDetailVo;
import com.mdd.admin.vo.yylq.TAppCityListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppCity;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppProvince;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppCityMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 【请填写功能名称】实现类
 * @author LikeAdmin
 */
@Service
public class TAppCityServiceImpl implements ITAppCityService {
        
    @Resource
    TAppCityMapper tAppCityMapper;

    /**
     * 【请填写功能名称】列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppCityListedVo>
     */
    @Override
    public PageResult<TAppCityListedVo> list(PageValidate pageValidate, TAppCitySearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppCityMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:provinceId@province_id:int",
        });

        IPage<TAppCity> iPage = tAppCityMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppCityListedVo> list = new LinkedList<>();
        for(TAppCity item : iPage.getRecords()) {
            TAppCityListedVo vo = new TAppCityListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 【请填写功能名称】详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppCity
     */
    @Override
    public TAppCityDetailVo detail(Integer id) {
        TAppCity model = tAppCityMapper.selectOne(
                new QueryWrapper<TAppCity>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppCityDetailVo vo = new TAppCityDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 【请填写功能名称】新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppCityCreateValidate createValidate) {
        TAppCity model = new TAppCity();
        model.setName(createValidate.getName());
        model.setProvinceId(createValidate.getProvinceId());
        tAppCityMapper.insert(model);
    }

    /**
     * 【请填写功能名称】编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppCityUpdateValidate updateValidate) {
        TAppCity model = tAppCityMapper.selectOne(
                new QueryWrapper<TAppCity>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setProvinceId(updateValidate.getProvinceId());
        tAppCityMapper.updateById(model);
    }

    /**
     * 【请填写功能名称】删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppCity model = tAppCityMapper.selectOne(
                new QueryWrapper<TAppCity>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppCityMapper.delete(new QueryWrapper<TAppCity>().eq("id", id));
    }

    @Override
    public List<TAppCity> selectCityList(Integer provinceId) {
        List<TAppCity> list = tAppCityMapper.selectList(new QueryWrapper<TAppCity>().eq("province_id", provinceId).orderByAsc("id"));
        return Optional.ofNullable(list).orElse(new ArrayList<>());
    }

}
